package com.knight.mall.service.impl;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.knight.mall.common.pageUtils.PageResult;
import com.knight.mall.exception.ServiceException;
import com.knight.mall.mappers.ShopPanicBuyingMapper;
import com.knight.mall.model.ShopPanicBuying;
import com.knight.mall.query.ShopPanicBuyingQuery;
import com.knight.mall.service.PanicBuyingProductService;
import com.knight.mall.service.PanicBuyingService;
import com.knight.mall.service.ProductService;
import com.knight.mall.service.ShopPanicBuyingService;

@Service(value = "shopPanicBuyingService")
public class ShopPanicBuyingServiceImpl implements ShopPanicBuyingService {

	private static final SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");

	@Autowired
	private ShopPanicBuyingMapper shopPanicBuyingMapper;

	@Value("${store.server.url}")
	private String storeUrl;

	@Autowired
	private PanicBuyingService panicBuyingService;

	@Autowired
	private ProductService productService;
	
	@Autowired
	private PanicBuyingProductService panicBuyingProductService;
	
	@Override
	public ShopPanicBuying load(Long id) {
		return shopPanicBuyingMapper.load(id);
	}

	@Override
	public Long add(ShopPanicBuying t) throws ServiceException {
		shopPanicBuyingMapper.insert(t);
		return 1L;
	}

	@Override
	public int update(ShopPanicBuying t) {
		shopPanicBuyingMapper.update(t);
		return 1;
	}

	@Override
	public int delete(Long id) {
		return shopPanicBuyingMapper.delete(id);

	}

	@Override
	public List<ShopPanicBuying> queryList(ShopPanicBuyingQuery q) {
		return shopPanicBuyingMapper.queryList(q);
	}

	@Override
	public int queryCount(ShopPanicBuyingQuery q) {
		return shopPanicBuyingMapper.queryCount(q);
	}

	/***
	 * 查询列表
	 * 
	 * @param q
	 * @return
	 */
	public PageResult<ShopPanicBuying> queryListForPage(ShopPanicBuyingQuery q) {
		/*List<ShopPanicBuying> list = this.queryList(q);
		if (list != null && list.size() > 0) {
			for (ShopPanicBuying s : list) {
				PanicBuying p = panicBuyingService.load(s.getPbId());
				if (p != null) {

					Integer num = p.getNum();
					Integer saleNum = p.getSaleNum();
					if (saleNum == null || saleNum == 0) {
						p.setShowNum("100%");
					} else {
						p.setShowNum(NumberUtil.get(saleNum, num));
					}
					ProductQuery pQuery = new ProductQuery();
					pQuery.setIdArray(p.getProductId());
					List<Product> listPS = productService.queryList(pQuery);
					Product pt = null;
					if (listPS != null && listPS.size() > 0) {
						pt = listPS.get(0);
					}
					s.setPanicBuying(p);
					s.setProduct(pt);
				}

				Date now = new Date();
				if (now.compareTo(s.getStartTime()) >= 0 && now.compareTo(s.getEndTime()) <= 0) {
					// 在中间
					s.setNowStatus("2");
				} else if (s.getEndTime().compareTo(now) < 0) {
					// 结束
					s.setNowStatus("1");
				} else if (s.getStartTime().compareTo(now) > 0) {
					// 即将开始
					s.setNowStatus("3");
				}

				s.setStrStartTime(format.format(s.getStartTime()));
				s.setStrEndTime(format.format(s.getEndTime()));

			}
		}*/

		return null;
	}

	@Override
	public List<ShopPanicBuying> queryListData() {
		ShopPanicBuyingQuery shopPanicBuyingQuery = new ShopPanicBuyingQuery();
		Calendar c = Calendar.getInstance();
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		shopPanicBuyingQuery.setStartTimeFrom(c.getTime());
		c.set(Calendar.HOUR_OF_DAY, 23);
		c.set(Calendar.MINUTE, 59);
		c.set(Calendar.SECOND, 59);
		shopPanicBuyingQuery.setEndTimeFrom(c.getTime());
		shopPanicBuyingQuery.setStatus(1);
		List<ShopPanicBuying> list = shopPanicBuyingMapper.queryListData(shopPanicBuyingQuery);
		if (list != null && list.size() > 0) {
			for (ShopPanicBuying s : list) {
				Date now = new Date();
				
				if (now.compareTo(s.getStartTime()) >= 0 && now.compareTo(s.getEndTime()) <= 0) {
					// 在中间
					s.setNowStatus("2");
				} else if (s.getEndTime().compareTo(now) < 0) {
					// 结束
					s.setNowStatus("1");
				} else if (s.getStartTime().compareTo(now) > 0) {
					// 即将开始
					s.setNowStatus("3");
				}
			}
		}
		return list;
	}
}
